using System;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Input;
using System.Collections.Generic;

namespace Nyanimals
{
	public class CRaceState : CBaseState
	{
		CIntLimiter focusNyanIndex;
        CWayPoint firstWayPoint;
        
		public CRaceState ()
		{
            focusNyanIndex = new CIntLimiter(-1, 0, -1, true);
		}
		
		public void Enter()
		{
            // waypoint positions
            int waypointCount = 4;
            Vector3[] positions = new Vector3[waypointCount];
            /*
            for(int wpIndex = 0; wpIndex < waypointCount; ++wpIndex)
            {
                positions[wpIndex].X = CRandom.Next(150);
                positions[wpIndex].Y = 0.0f;
                positions[wpIndex].Z = CRandom.Next(150);
            }*/
            
            positions[0] = new Vector3(0, 0, 30);
            positions[1] = new Vector3(30, 0, 30);
            positions[2] = new Vector3(30, 0, 0);
            positions[3] = new Vector3(0, 0, 0);
            
            
            // create waypoints
            CWayPoint newPoint = new CWayPoint(0, positions[0], 3.0f);
            float waypointRadius = 5.0f;
            firstWayPoint = newPoint;
            for(int pointCount = 1; pointCount < waypointCount; ++pointCount)
            {
                newPoint.Next = new CWayPoint(pointCount, positions[pointCount], waypointRadius);
                newPoint.Next.Prev = newPoint;
                newPoint = newPoint.Next;
                
                if(pointCount == waypointCount - 1)
                {
                    newPoint.Next = firstWayPoint;
                }
                
            }
            
            
            // place Nyanimals
            int count = 1;
            int spread = 50 * count;
            for(int i = 0; i < count; ++i)
            {
                Vector3 pos = new Vector3();
                pos.X = 0 + i;
                pos.Y = 0.0f;
                pos.Z = 0.0f;
                CNyanimal nyan = CObjectManager.Instance.CreateNyanimal(Matrix4.Translation(pos));
                nyan.LookAt( pos + Vector3.UnitZ, Vector3.UnitY);
                
                nyan.Hunger = (float)CRandom.Next(0, 100);
                nyan.StateMachine.ChangeState("idle");
                ((CNyanimalIdleState)nyan.StateMachine.CurState).Stuck = true;
            }
            
            focusNyanIndex.Max = CObjectManager.Instance.NyanList.Count - 1;
            focusNyanIndex.Value = -1;
		}
		
		public void Exit()
		{
		}
		
		public void Input()
		{
			CObjectManager.Instance.Input();
			
            if(CInput.Instance.ButtonPressed(GamePadButtons.L))
            {
                // camera on prev Nyanimal
                focusNyanIndex.Value -= 1;
            }
            if(CInput.Instance.ButtonPressed(GamePadButtons.R))
            {
                // camera on next Nyanimal
                focusNyanIndex.Value += 1;
            }
            
            // TODO: Make as a timer not a button press
            if(CInput.Instance.ButtonPressed(GamePadButtons.Start))
            {
                CEventManager.Instance.QueueEvent(new CStartRaceEvent(firstWayPoint));
            }
            
		}
		
		public void Update(float deltaTime)
		{
			CObjectManager.Instance.Update(deltaTime);
			
			if(focusNyanIndex.Value >= 0)
            {
                CObjectManager.Instance.UpdateCamAroundObject(CObjectManager.Instance.NyanList[focusNyanIndex.Value], deltaTime);
            }
            else
            {
                CObjectManager.Instance.UpdateCamAroundObject(null, deltaTime);
            }
            
            CWayPoint curPoint = firstWayPoint;
            while(curPoint != null && curPoint.MyIndex > curPoint.Next.MyIndex)
            {
                curPoint.DebugUpdate(deltaTime);
                curPoint = curPoint.Next;
            }
            
            // end of frame stuff
            CEventManager.Instance.ProcessEvents();
            
		}
		
		public void Render()
		{
		}
		
		public StateTypes GetStateType()
		{
			return StateTypes.Game_State;
		}
	}
}

